﻿using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace FTBS.Models.DAO
{
    public class LevelDAO
    {
        private FilmTicketBookingSystemEntities _entities = new FilmTicketBookingSystemEntities();

        /// <summary>
        /// Load all Level
        /// </summary>
        /// <returns>List of Level</returns>
        /// <author>VuongVT</author>
        public List<Level> LoadLevels()
        {
            try
            {
                List<Level> listLevel = new List<Level>();
                var levels =  from level in _entities.Levels
                              where level.IsDeleted == false
                              orderby level.LevelId descending
                              select level;
                if (levels != null)
                {
                    listLevel = levels.ToList();
                }
                return listLevel;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Load Level By Id
        /// </summary>
        /// <returns>Level</returns>
        /// <author>VuongVT</author>
        public Level LoadLevelsById(int id)
        {
            try
            {
                Level level = new Level();
                level = (from levelEnt in _entities.Levels
                          where levelEnt.LevelId == id
                             && levelEnt.IsDeleted == false
                         select levelEnt).FirstOrDefault();
                return level;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Add a Level
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>VuongVT</author>
        public int Add(Level level)
        {
            try
            {
                _entities.Levels.Add(level);
                int resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Edit a Level
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>VuongVT</author>
        public int Edit(Level level)
        {
            try
            {
                _entities.Entry(level).State = EntityState.Modified;
                int resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Delete a Level
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>VuongVT</author>
        public int Delete(Level level)
        {
            try
            {
                level.IsDeleted = true;
                _entities.Entry(level).State = EntityState.Modified;
                int result = _entities.SaveChanges();
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Check if level is existed or not
        /// </summary>
        /// <param name="lev"></param>
        /// <returns>true || false</returns>
        /// <author>duyhdv</author>
        /// <date>2014/03/3</date>
        public bool CheckIsExisted(Level lev)
        {
            try
            {
                var isExist = _entities.Levels.Any(l => l.LevelName == lev.LevelName);
                return isExist;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Level GetLevelById(int? id)
        {
            var level = _entities.Levels.Find(id);
            return level;
        }

    }
}